<template>
  <div class="adminhouse">
    <div class="header-container">
      <h2 class="page-title">房源管理</h2>
      <el-button class="add-btn" size="medium" @click="gotoRelease">
        <i class="el-icon-plus"></i> 发布新房源
      </el-button>
    </div>
    
    <div v-if="allList.length > 0" class="content-area">
      <div class="houses-grid">
        <div class="house-card" v-for="item in list" :key="item.id">
          <House :house="item" :get-house-info="getHouseInfo" :Page="currentPage"></House>
        </div>
      </div>
      
      <div class="pagination-container">
        <el-pagination 
          @current-change="handlePage" 
          :current-page.sync="currentPage" 
          layout="prev, pager, next"
          :total="allList.length" 
          :page-size="9"
          background>
        </el-pagination>
      </div>
    </div>
    
    <div v-else class="empty-state">

      <p class="empty-message">暂无房源数据</p>
      <el-button type="primary" @click="gotoRelease">发布第一个房源</el-button>
    </div>
  </div>
</template>
<script>
import House from '@/components/houseset.vue'
export default {
  name: 'AdminHouse',
  created() {
    this.getHouseInfo();
  },
  components: {
    House
  },
  data() {
    return {
      //当前页
      currentPage: 1,
      allList: [],
      list: [],
    }
  },
  methods: {
    //分页
    handlePage(page) {
      const start = (page - 1) * 9;
      const end = page * 9;
      this.list = this.allList.slice(start, end);
    },
    //跳转到发布房子页面
    gotoRelease() {
      this.$router.push('/index/admin/release')
    },
    //获取房子信息
    async getHouseInfo(page = 1) {
      if (this.$store.getters.getUserInfo.role === 'owner') {
        const { data: res } = await this.$axios.get(`/api/house/owner/${this.$store.getters.getUserInfo.id}`)
        if (res.status === 200) {
          this.allList = res.data;
          this.list = this.allList.slice(0, 9);
          this.handlePage(page)
        }
      } else {
        const { data: res } = await this.$axios.get(`/api/house/list`)
        if (res.status === 200) {
          this.allList = res.data;
          this.list = this.allList.slice(0, 9);
          this.handlePage(page)
        }
      }
    }
  }
}
</script>

<style scoped>
.adminhouse {
  padding: 24px 48px;
  min-height: calc(100vh - 64px);
  background-color: #f8fafc;
}

.header-container {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 32px;
  padding-bottom: 24px;
  border-bottom: 1px solid #eaecef;
}

.page-title {
  font-size: 24px;
  font-weight: 600;
  color: #2c3e50;
  margin: 0;
}

.add-btn {
  background-color: #4a5eff;
  color: white;
  border: none;
  border-radius: 6px;
  padding: 10px 20px;
  transition: all 0.3s;
}

.add-btn:hover {
  background-color: #3a4de8;
  box-shadow: 0 2px 12px rgba(74, 94, 255, 0.2);
  transform: translateY(-2px);
}

.add-btn i {
  margin-right: 6px;
}

.houses-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 24px;
  margin-bottom: 32px;
}

.house-card {
  flex: 1 1 calc(33.333% - 24px);
  min-width: 300px;
  transition: transform 0.3s;
}

.house-card:hover {
  transform: translateY(-5px);
}

.pagination-container {
  display: flex;
  justify-content: center;
  margin-top: 40px;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 80px 0;
  text-align: center;
}

.empty-image {
  width: 180px;
  height: 180px;
  margin-bottom: 24px;
  opacity: 0.7;
}

.empty-message {
  font-size: 16px;
  color: #909399;
  margin-bottom: 24px;
}

@media (max-width: 768px) {
  .adminhouse {
    padding: 16px;
  }
  
  .header-container {
    flex-direction: column;
    align-items: flex-start;
  }
  
  .page-title {
    margin-bottom: 16px;
  }
  
  .houses-grid {
    flex-direction: column;
  }
  
  .house-card {
    width: 100%;
    min-width: auto;
  }
}
</style>